#!/usr/bin/env node

/**
 * 批量更新项目中所有使用渠道筛选的页面
 * 将它们从使用getChannelList改为使用channelMixin
 */

const fs = require('fs')
const path = require('path')

// 需要更新的文件列表
const filesToUpdate = [
  'src/views/withdrawal/WithdrawalStatistics.vue',
  'src/views/withdrawal/WithdrawalRecords.vue',
  'src/views/payment/PaymentRecords.vue',
  'src/views/machine-stats/MachineHistory.vue',
  'src/views/machine-stats/MachineDetail.vue',
  'src/views/machine-stats/MachineDailyProfit.vue',
  'src/views/PlayerInfo.vue',
  'src/views/OnlineInfo.vue',
  'src/views/CdkeyManagement.vue'
]

// 更新规则
const updateRules = [
  // 移除getChannelList导入
  {
    from: /import.*getChannelList.*from.*\n/g,
    to: ''
  },
  // 添加channelMixin导入
  {
    from: /(import.*from.*\n)(\nexport default \{)/g,
    to: '$1import channelMixin from \'@/mixins/channelMixin\'\n$2\n  mixins: [channelMixin],'
  },
  // 移除channelOptions数据定义
  {
    from: /,?\s*channelOptions:\s*\[\]/g,
    to: ''
  },
  // 移除loadChannelOptions方法
  {
    from: /async loadChannelOptions\(\).*?\},\s*/gs,
    to: ''
  },
  // 更新渠道参数格式化
  {
    from: /channel:\s*this\..*channel.*?\?\s*.*?\(this\..*channel.*?\)\s*:\s*0/g,
    to: 'channel: this.formatChannelParam(this.filterForm.channel)'
  }
]

function updateFile(filePath) {
  console.log(`正在更新文件: ${filePath}`)
  
  if (!fs.existsSync(filePath)) {
    console.log(`文件不存在，跳过: ${filePath}`)
    return
  }
  
  let content = fs.readFileSync(filePath, 'utf8')
  let originalContent = content
  
  // 应用更新规则
  updateRules.forEach(rule => {
    content = content.replace(rule.from, rule.to)
  })
  
  // 检查是否有变化
  if (content !== originalContent) {
    fs.writeFileSync(filePath, content, 'utf8')
    console.log(`✓ 已更新: ${filePath}`)
  } else {
    console.log(`- 无需更新: ${filePath}`)
  }
}

function main() {
  console.log('开始批量更新渠道筛选页面...\n')
  
  filesToUpdate.forEach(updateFile)
  
  console.log('\n批量更新完成！')
  console.log('\n注意事项：')
  console.log('1. 请检查每个文件的更新结果')
  console.log('2. 如果有复杂的自定义逻辑，可能需要手动调整')
  console.log('3. 建议运行测试确保功能正常')
}

if (require.main === module) {
  main()
}

module.exports = { updateFile, updateRules } 